<!-- 购物车列表弹窗 -->
<template>
  <div id="yz-cartList">
    <van-popup v-model="showCarList" :close-on-click-overlay="false" round position="bottom">
      <div class="yz-cartList-top">
        <div class="text">
          已选择商品 <em style="color: #999;">（共{{ cardata.length }}件）</em>
        </div>
        <div class="clone-box">
          <div class="destoryCart" v-if="showDestory" @click="clearCart('all')">
            <i class="iconfont icon-fontclass-shanchu"></i>
            清空购物车</div>
          <i class="iconfont icon-close11 " @click.stop="closedCarList"></i>
        </div>
      </div>
      <div class="yz-cartList-main">
        <div class="yz-cartList-list" v-for="item in cardata" :key="item.id" v-show="!item.is_failure">
          <!-- 商品图片隐藏（PS:设计稿没有），后面如有添加直接去掉if条件 -->
          <template v-if="item.goods">
            <img v-if="goodImg" :src="item.option_id != 0 ? item.goods_option.thumb : item.goods.thumb" alt="" class="g-img" />
            <div class="g-info">
              <div class="g-title">{{ item.goods.title }}</div>
              <div class="g-option">{{ item.option_str }}</div>
              <div class="g-price" :style="{ color: `${themeColor}` }">
                <em>{{ $i18n.t("money") }}</em>
                {{ item.goods_option && item.option_id != 0 ? item.goods_option.product_price : item.goods.price }}
              </div>
            </div>
          </template>
          <template v-else-if="item.goods_id">
            <img :src="item.goods_thumb" alt="" class="g-img" />
            <div class="g-info">
              <div class="g-title">{{ item.goods_title }}</div>
              <div class="g-option">{{ item.goods_option_title }}</div>
              <div class="g-price">
                <em>{{ $i18n.t("money") }}</em>
                {{ item.goods_price }}
              </div>
            </div>
          </template>
          <div class="add-cart-btn">
            <van-stepper
              :style="{ '--color': `${themeColor}` }"
              v-model.number="item.total"
              theme="round"
              button-size="22"
              integer
              min="0"
              @focus="devEvents('focus', item.id || item.cart_id, item.total)"
              @blur="devEvents('blur', item.id|| item.cart_id, item.total)"
              @plus="devEvents('plus', item.id|| item.cart_id, item.total)"
              @minus="devEvents('minus', item.id|| item.cart_id, item.total)"
            />
          </div>
        </div>
        <div class="lineBox" v-if="checkFail"></div>
        <div class="failBox" v-if="checkFail">
          <div class="failBoxL">失效商品</div>
          <div class="failBoxR" @click="clearCart('fail')">
            <i class="iconfont icon-delete"></i>
            清空失效</div>
        </div>
        <div class="yz-cartList-list" v-for="item in cardata" :key="item.id" v-if="item.is_failure == 1">
          <!-- 商品图片隐藏（PS:设计稿没有），后面如有添加直接去掉if条件 -->
          <div class="goods_img">
            <img v-if="goodImg" :src="item.option_id != 0 ? item.goods_option.thumb : item.goods.thumb" alt="" class="g-img op" />
            <div class="txt">已下架</div>
          </div>
          <div class="g-info op flex">
            <div class="g-title">{{ item.goods.title }}</div>
            <div class="g-option">{{ item.option_str }}</div>
            <div class="g-price" :style="{ color: `${themeColor}` }">
              <em>{{ $i18n.t("money") }}</em>
              {{ item.goods_option && item.option_id != 0 ? item.goods_option.product_price : item.goods.price }}
            </div>
          </div>
        </div>
      </div>
      <!-- 底部购物车按钮 -->
      <div style="margin: 0.5rem 0;">
        <slot></slot>
      </div>
    </van-popup>
  </div>
</template>

<script>
export default {
  model: {
    prop: "showCarList",
    event: "onEmit"
  },
  props: {
    //弹窗是否显示，默认不显示
    showCarList: {
      type: Boolean,
      default: false
    },
    cardata: {
      type: Object,
      default: () => ({})
    },
    //主题色
    themeColor: {
      type: String,
      default: "var(--themeBaseColor)"
    },
    goodImg:{
      type:Boolean,
      default:false
    },
    showDestory:{
      type:Boolean,
      default:false
    }
  },
  data() {
    return {
      timer: null
    };
  },
  watch:{
    showCarList(nval,oval){
      console.log(nval,oval)
    }
  },
  components: {},
  computed: {
    checkFail:function(){
      let bool = false;
      if(this.cardata.length > 0){
        this.cardata.forEach((ele,ind) => {
          if(ele.is_failure == 1){
            bool = true;
          }
        });
        return bool
      }else{
        return bool
      }
    },
  },
  methods: {
    devEvents(name, id, total) {
      if (this.timer) clearTimeout(this.timer); //防抖
      this.timer = setTimeout(() => {
        this.$emit(name, { id, total, name });
      }, 500);
    },
    closedCarList() {
      this.$emit("onEmit", false);
    },
    clearCart(key){
      this.$emit('clearCart',key)
    }
  }
};
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
#yz-cartList {
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  .clone-box{
    display: flex;
    justify-content: flex-end;
    .icon-close11{
      margin-left: 1rem;
    }
  }
  .destoryCart{
    font-size: 0.75rem;
    color: #AAAAB3;
    display: flex;
    align-items: center;
    justify-content: center;
    .iconfont{
      margin-right: 0.13rem;
    }
  }
  .yz-cartList-top {
    display: flex;
    padding: 0 0.875rem;
    justify-content: space-between;
    height: 3.25rem;
    line-height: 3.25rem;

    .text {
      font-size: 0.875rem;
      color: #333;
    }
  }

  .yz-cartList-main {
    flex: 1;
    max-height: 19rem;
    overflow-y: scroll;
    padding: 0 0.875rem;
  }

  .yz-cartList-list {
    border-top: 1px solid #f2f2f2;
    display: flex;
    justify-content: space-between;
    padding: 0.875rem 0;
    .goods_img{
      position: relative;
    }
    .g-img {
      width: 4.125rem;
      height: 4.125rem;
      border-radius: 0.5rem;
      overflow: hidden;
      flex-shrink: 0;
      margin: 0;
      margin-right: 0.5rem;
    }
    .txt{
      position: absolute;
      top: 1.25rem;
      left: 0.38rem;
      height: 1.25rem;
      width: 70%;
      border-radius: 0.94rem;
      z-index: 999;
      color: #fff;
      background: #000000;
      font-size: 0.69rem;
      line-height: 1.25rem;
    }

    .g-info {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      overflow: hidden;
      text-align: left;

      .g-title {
        width: 100%;
        font-size: 0.875rem;
        font-weight: bold;
        overflow: hidden; /* 超出部分隐藏 */
        text-overflow: ellipsis; /* 超出部分显示省略号 */
        white-space: nowrap; /* 规定段落中的文本不进行换行 */
        padding-bottom: 0.5rem;
      }

      .g-option {
        font-size: 0.75rem;
        color: #999;
        padding-bottom: 0.5rem;
      }

      .g-price {
        font-size: 1.125rem;
        color: var(--themeBaseColor);
        font-weight: bold;

        em {
          font-size: 0.75rem;
        }
      }
    }

    .add-cart-btn {
      display: flex;
      flex-direction: column-reverse;

      .van-stepper {
        ::v-deep .van-stepper__minus {
          color: var(--themeBaseColor);
          border-color: var(--themeBaseColor);
        }

        ::v-deep .van-stepper__plus {
          background-color: var(--themeBaseColor);
        }
      }
    }
  }

  .yz-cartList-addcart-btn {
    width: 100%;
    height: 4.5rem;
    display: flex;
    align-items: center;
    background: linear-gradient(180deg, hsla(0, 0%, 100%, 0), #fff 19%);
    background-color: transparent;

    .car-box {
      border: 1px solid var(--themeBaseColor);
      border-radius: 1.5rem;
      display: flex;
      align-items: center;
      height: 3rem;
      line-height: 3rem;
      width: 90%;
      margin: 0 auto;
      overflow: hidden;

      .car-num {
        width: 50%;
        text-indent: 1.25rem;
        text-align: left;
        color: var(--themeBaseColor);
        display: flex;

        i {
          font-size: 1.5rem;
        }

        .small-price {
          font-size: 0.75rem;
        }

        font-size: 1.125rem;
      }

      .car-add {
        flex: 1;
        text-align: center;
        background-color: var(--themeBaseColor);
        color: #fff;
      }
    }
  }
}
.lineBox {
  height: 0.63rem;
  background: #F5F5F5;
}
.failBox {
  background: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.94rem 0.75rem;
  padding-bottom: 0.4rem;
  .failBoxL {
    color: #3B3B4A;
    font-size: 0.9rem;
  }
  .failBoxR {
    color: #6E6E79;
    font-size: 0.8rem;
    display: flex;
    align-items: center;
  }
}
.op {
  opacity: 0.3;
}
.opf {
  flex-direction: column;
  align-items: start !important;
}
.opf_m{
  margin-top: 0 !important;
}
</style>
